package gunfire.relics;

import basemod.abstracts.CustomRelic;
import com.badlogic.gdx.graphics.Texture;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.ClearCardQueueAction;
import com.megacrit.cardcrawl.actions.animations.TalkAction;
import com.megacrit.cardcrawl.actions.common.*;
import com.megacrit.cardcrawl.actions.utility.LoseBlockAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.BarricadePower;
import com.megacrit.cardcrawl.powers.DexterityPower;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import gunfire.GunfireMod;
import gunfire.cards.Rusty;
import gunfire.util.TextureLoader;

import java.util.ArrayList;

import static gunfire.GunfireMod.makeRelicOutlinePath;
import static gunfire.GunfireMod.makeRelicPath;

public class SoulOfIron2 extends CustomRelic {

    public static final String ID = GunfireMod.makeID("SoulOfIron2");
    private static final Texture IMG = TextureLoader.getTexture(makeRelicPath("SoulOfIron2.png"));
    private static final Texture OUTLINE = TextureLoader.getTexture(makeRelicOutlinePath("blank.png"));
    private boolean isFirstTurn;
    private int maxTurn;

    public SoulOfIron2() {
        super(ID, IMG, OUTLINE, RelicTier.SPECIAL, LandingSound.CLINK);
        this.counter = 0;
        this.isFirstTurn = false;
        this.pulse = false;
        this.maxTurn = 3;
    }

    @Override
    public String getUpdatedDescription() {
        return DESCRIPTIONS[0];
    }

    private void calcMaxTurn(){
        this.maxTurn = 3;
        if (AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT){
            this.maxTurn = AbstractDungeon.getCurrRoom().eliteTrigger ? 5 : 3;
            for(AbstractMonster m : AbstractDungeon.getCurrRoom().monsters.monsters){
                if (m.type == AbstractMonster.EnemyType.BOSS) {
                    this.maxTurn = 7;
                    break;
                }
            }
        }
    }

    @Override
    public void obtain() {
        if (AbstractDungeon.player.hasRelic("gunfire:SoulOfIron")) {
            for(int i = 0; i < AbstractDungeon.player.relics.size(); ++i) {
                if (AbstractDungeon.player.relics.get(i).relicId.equals(SoulOfIron.ID)) {
                    this.instantObtain(AbstractDungeon.player, i, true);
                    break;
                }
            }
        } else {
            super.obtain();
        }
    }

    @Override
    public void onEquip() {
        AbstractDungeon.player.energy.energy++;
        AbstractDungeon.player.energy.energyMaster++;
        int maxHp = AbstractDungeon.player.maxHealth;
        AbstractDungeon.player.decreaseMaxHealth(maxHp - 1);
        if(AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT){
            this.flash();
            this.calcMaxTurn();
            this.addToBot(new GainBlockAction(AbstractDungeon.player, AbstractDungeon.player, 15));
            this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new DexterityPower(AbstractDungeon.player, 3), 3));
            this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new BarricadePower(AbstractDungeon.player)));
            this.addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));

            //疼痛、悔恨变成生锈
            ArrayList<AbstractCard> removes1 = new ArrayList<>();
            for(AbstractCard c : AbstractDungeon.player.drawPile.group){
                if(c.cardID.equals("Pain") || c.cardID.equals("Regret")){
                    removes1.add(c);
                }
            }
            for (AbstractCard c : removes1){
                AbstractDungeon.player.drawPile.removeCard(c);
                AbstractDungeon.player.drawPile.addToRandomSpot(new Rusty());
            }

            ArrayList<AbstractCard> removes2 = new ArrayList<>();
            for(AbstractCard c : AbstractDungeon.player.hand.group){
                if(c.cardID.equals("Pain") || c.cardID.equals("Regret")){
                    removes2.add(c);
                }
            }
            for (AbstractCard c : removes2){
                AbstractDungeon.player.hand.removeCard(c);
                AbstractDungeon.player.hand.addToHand(new Rusty());
            }

            for(AbstractCard c : AbstractDungeon.player.discardPile.group){
                if(c.cardID.equals("Pain") || c.cardID.equals("Regret")){
                    removes1.add(c);
                }
            }
            for (AbstractCard c : removes1){
                AbstractDungeon.player.discardPile.removeCard(c);
                AbstractDungeon.player.discardPile.addToRandomSpot(new Rusty());
            }
        }
    }

    @Override
    public void onUnequip() {
        AbstractDungeon.player.energy.energyMaster--;
    }

    @Override
    public void atBattleStart() {
        this.flash();
        this.counter = 0;
        this.isFirstTurn = true;
        this.pulse = false;
        this.calcMaxTurn();
        this.addToBot(new GainBlockAction(AbstractDungeon.player, AbstractDungeon.player, 15));
        this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new DexterityPower(AbstractDungeon.player, 5), 5));
        this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new BarricadePower(AbstractDungeon.player)));
        this.addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));

        //疼痛、悔恨变成生锈
        ArrayList<AbstractCard> removes = new ArrayList<>();
        for(AbstractCard c : AbstractDungeon.player.drawPile.group){
            if(c.cardID.equals("Pain") || c.cardID.equals("Regret")){
                removes.add(c);
            }
        }
        for (AbstractCard c : removes){
            AbstractDungeon.player.drawPile.removeCard(c);
            AbstractDungeon.player.drawPile.addToRandomSpot(new Rusty());
        }
    }

    @Override
    public void atTurnStart() {
        if(this.isFirstTurn){
            this.isFirstTurn = false;
        } else {
            this.counter++;
            this.addToBot(new LoseBlockAction(AbstractDungeon.player, AbstractDungeon.player, AbstractDungeon.player.currentBlock / 2));
            if(this.counter == this.maxTurn){
                for (AbstractMonster m : AbstractDungeon.getCurrRoom().monsters.monsters){
                    if(!m.isDeadOrEscaped()){
                        this.addToBot(new TalkAction(m, Settings.language == Settings.GameLanguage.ZHS ? "打不动，溜了溜了" : "Unbreakable! I'm running away."));
                        this.addToBot(new EscapeAction(m));
                    }
                }
                //this.addToBot(new WaitAction(2.0F));
                this.addToBot(new ClearCardQueueAction());
                this.addToBot(new AbstractGameAction() {
                    @Override
                    public void update() {
                        AbstractDungeon.actionManager.clear();
                        AbstractDungeon.getCurrRoom().isBattleOver = true;
                        AbstractDungeon.player.onVictory();
                    }
                });
                this.counter = 0;
                this.isFirstTurn = true;
                this.pulse = false;
            } else if(this.counter == this.maxTurn - 1){
                this.beginPulse();
                this.pulse = true;
            }
        }
    }

    @Override
    public int onAttackToChangeDamage(DamageInfo info, int damageAmount) {
        if (info.owner != null && AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT && info.type != DamageInfo.DamageType.HP_LOSS && info.type != DamageInfo.DamageType.THORNS && damageAmount > 0) {
            this.flash();
            this.addToTop(new GainBlockAction(AbstractDungeon.player, AbstractDungeon.player, damageAmount));
            return 0;
        } else {
            return damageAmount;
        }
    }

    @Override
    public AbstractRelic makeCopy() {
        return new SoulOfIron2();
    }
}
